package com.teatime.client.bus.publish;

import java.util.ArrayList;
import java.util.List;

import com.teatime.client.R;
import com.teatime.client.activity.TActivity;
import com.teatime.client.image.photomgr.PhotoMgrView;
import com.teatime.client.view.FormRow;
import com.teatime.client.view.PopupEditor;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public abstract class Publisher extends TActivity implements OnClickListener {
	public final static String SUBJECT    = "pub.subjuect";
	public final static String PRICE      = "pub.price";
	public final static String PRICE_ORIG = "pub.price_orig";
	public final static String DETAILS    = "pub.details";
	public final static String PHOTOS     = "pub.phtotos";
	
	private List<PopupEditor>  mEditors;
	
	protected PhotoMgrView   mPhotoMgrView;
	protected Bundle         mBundle;
	
	protected void addRow(int rowId) {
		FormRow row = (FormRow)findViewById(rowId);
		if (row.getPopupEditor() != null) {
			mEditors.add(row.getPopupEditor());
		}
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getContentViewId());
		
		mBundle = new Bundle();
		mEditors = new ArrayList<PopupEditor>();
		initBottomButton(R.id.btn_pub_save);
		initBottomButton(R.id.btn_pub_submit);
		
		mPhotoMgrView = (PhotoMgrView)findViewById(R.id.pub_photo_mgr);
		
		addRow(R.id.row_pub_subject);
		addRow(R.id.row_pub_details);
	}
	
	private void initBottomButton(int btnId) {
		Button button = (Button)findViewById(btnId);
		if (button != null) {
			button.setOnClickListener(this);
		}
		
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
		if (mPhotoMgrView.onActivityResult(requestCode, resultCode, intent)) {
			return;
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id) {
		case R.id.btn_pub_save:
			onSave();
			break;
		case R.id.btn_pub_submit:
			onSubmit();
			break;
		}
		
	}
	
	private void saveToBundle() {
		int i;
		mBundle.clear();
		// put photos into mBundle
		
		// 
		for (i = 0; i < mEditors.size(); i ++) {
			PopupEditor editor = mEditors.get(i);
			if (editor != null) {
				editor.onSaveData(mBundle);
			}
		}
	}
	
	protected void onSave() {
		saveToBundle();
	}
	
	protected void onSubmit() {
		saveToBundle();
	}

}
